package com.apes.scm.stock.transfer.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.product.service.UomService;
import com.apes.scm.pur.purchaseReturn.model.PurchaseReturn;
import com.apes.scm.pur.purchaseReturn.model.PurchaseReturnItem;
import com.apes.scm.pur.purchaseReturn.repository.PurchaseReturnRepository;
import com.apes.scm.stock.transfer.model.Pick;
import com.apes.scm.voucher.model.Voucher;
import com.apes.scm.voucher.model.VoucherItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/*
 * 流转到返厂凭证(未回写返厂订单发货状态,开票状态)
* */
@Service("pickFlowPurchaseReturnVoucherService")
public class PickFlowPurchaseReturnVoucherService extends DomainService {
    @Autowired
    PurchaseReturnRepository purchaseReturnRepository;

    @Autowired
    UomService uomService;


    public Voucher flowPurchaseReturnVoucher(SimpleRequest simpleRequest) {
        Pick pick = simpleRequest.getO(Pick.class);
        PurchaseReturn purchaseReturn = purchaseReturnRepository.findOne(pick.getOrigin());
        if (purchaseReturn == null) throw new RuntimeException(pick.getOrigin() + "返厂订单不存在！");
        Voucher voucher = new Voucher();
        // 主表信息
        voucher.setCompany(purchaseReturn.getCompany());
        voucher.setDept(purchaseReturn.getDept());
        voucher.setClerk(purchaseReturn.getClerk()); //业务员
        voucher.setContract(purchaseReturn.getContract());//采购合同
        voucher.setPartner(purchaseReturn.getSupplier());
        voucher.setInventoryMode(purchaseReturn.getCooperation().getInventoryMode());
        voucher.setAccountingMethod(purchaseReturn.getCooperation().getAccountingMethod());
        voucher.setScene(purchaseReturn.getScene());
        voucher.setOrigin(purchaseReturn.getId());
        voucher.setOriginBusinessType(pick.getOriginBusinessType());
        voucher.setSource(pick.getId());
        voucher.setSourceBusinessType(pick.getInnerBusinessType());
        // 退回标记
        voucher.setReturned(true);
        // 从表信息
        voucher.setVoucherItems(getPurchaseReturnVoucherItems(purchaseReturn, pick));

        // 更新发货状态
        purchaseReturn.setShipped();

        return voucher;
    }

    public Set<VoucherItem> getPurchaseReturnVoucherItems(PurchaseReturn purchaseReturn, Pick pick) {
        Set<VoucherItem> voucherItems = new HashSet();
        Map<Long, PurchaseReturnItem> PurchaseReturnItemMap = purchaseReturn.getPurchaseReturnItems().stream().collect(Collectors.toMap(PurchaseReturnItem::getId, purchaseReturnItem -> purchaseReturnItem));
        pick.getPickItems().forEach(
                (pickItem) -> {
                    VoucherItem voucherItem = new VoucherItem();
                    PurchaseReturnItem purchaseReturnItem = PurchaseReturnItemMap.get(Long.valueOf(pickItem.getOriginLine()));
                    if (purchaseReturnItem == null) {
                        throw new RuntimeException("未找到采购订单行：" + pickItem.getOriginLine());
                    }
                    voucherItem.setProduct(pickItem.getProduct());
                    voucherItem.setTax(purchaseReturnItem.getTax());
                    voucherItem.setOwner(purchaseReturn.getLocation().getCompany());
                    voucherItem.setUom(uomService.getUom(purchaseReturn.getCompany(), purchaseReturnItem.getProduct()));
                    // 回写发货数量
                    purchaseReturnItem.delivery(pickItem.getQtyDone());
                    // 为负数
                    voucherItem.setProductQty(-1 * purchaseReturnItem.computeQtyUnit(pickItem.getQtyDone()));
                    voucherItem.setPriceUnit(purchaseReturnItem.computePriceUnit());
                    voucherItem.setOriginItemId(String.valueOf(purchaseReturnItem.getId()));
                    voucherItem.setSourceItemId(String.valueOf(pickItem.getId()));
                    // 制定批次退货
                    voucherItem.setInventoryBatch(purchaseReturnItem.getInventoryBatch());
                    voucherItems.add(voucherItem);
                }
        );
        return voucherItems;
    }

}
